#include "tcpclient.h"
#include "ui_tcpclient.h"

#include <QMessageBox>

tcpClient::tcpClient(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::tcpClient)
{
    ui->setupUi(this);
    m_tcpsocket = new QTcpSocket(this);
}

tcpClient::~tcpClient()
{
    delete ui;
}


void tcpClient::on_openBt_clicked()
{
    m_tcpsocket->connectToHost(ui->lineEditIP->text(), ui->lineEditPort->text().toUInt());
    connect(m_tcpsocket, &QTcpSocket::connected, this, &tcpClient::connected_Slot);
}
void tcpClient::connected_Slot()
{
    QMessageBox::warning(this, "连接成功", "服务器检测到新连接");
    //    m_tcpsocket = m_tcpserver->nextPendingConnection();
    connect(m_tcpsocket, &QTcpSocket::readyRead, this, &tcpClient::readyRead);
}
void tcpClient::readyRead()
{
    //    QString text = m_tcpsocket->readAll();
    //    ui->textEditRev->append(text);
    ui->textEditRev->append(m_tcpsocket->readAll());
}

void tcpClient::on_closeBt_clicked()
{
    m_tcpsocket->close();
}

void tcpClient::on_Send_clicked()
{
    m_tcpsocket->write(ui->textEditSend->toPlainText().toUtf8());
    ui->textEditSend->clear();
}
